#########################################################################################
# Title: Figure3.R
# Author: Yuki Atsusaka (atsusaka@rice.edu)
# Aim: Code to replicate Figure 3 in Atsusaka (2021)
#########################################################################################

# CLEAN THE GLOBAL ENVIRONMENT AND READ PACKAGES
rm(list=ls())
library(plotly)
library(tidyverse)
library(RColorBrewer)
require("grDevices")

##############################################################
# COMPUTING MODLE PREDICTIONS AND READING OBSERVED dtA
##############################################################

# COMPUTING MODEL PREDICTIONS
M_vec <- seq(from=0, to=100, by=1)     # Vector of M: Racial margin of victory
C_vec <- seq(from=0, to=100, by=1)     # Vector of C: % minority voters
Mt <- matrix(NA, ncol=101, nrow=101)   # Matrix for storing model predictions
                                       # row = C, column = M
for(j in seq_along(M_vec)){  # Loop over M
 M <- M_vec[j]

for(i in seq_along(C_vec)){  # Loop over C
 C <- C_vec[i]   
 q <- sqrt(M*C) - 50

Mt[i,j] <- pnorm(q=q, mean=0, sd=1)    # Model prediction for (i,j)-th cell

}
}

# READING OBSERVED MINORITY CANDIDATE EMERGENCE
dt <- read_csv(here::here("Data_LAMayoral.csv"))


##############################################################
# VISUALIZING MODLE PREDICTIONS
##############################################################

filled.contour(x=M_vec,y=C_vec,z=Mt, 
               xlim=c(-2,102), ylim=c(0,100),
               plot.axes =
                 {points(dt$M[dt$run==0], dt$C[dt$run==0],pch=4)
                  points(dt$M[dt$run==1], dt$C[dt$run==1],pch=1)
                  points(dt$M[dt$run==0 & dt$NOLA==1], 
                         dt$C[dt$run==0 & dt$NOLA==1],pch=4, col="white", cex=1.2)
                  points(dt$M[dt$run==1 & dt$NOLA==1], 
                         dt$C[dt$run==1 & dt$NOLA==1],pch=1, col="white", cex=1.2)
                  axis(1, seq(0, 100, by = 25))
                  axis(2, seq(0, 100, by = 20))
                  text(75,11, labels="Black Candidte", font=2, cex=1.2, col="white")
                  text(78,5, labels="No Black Candidte", font=2, cex=1.2, col="white")
                  text(16, 95, labels=expression(P[run] < 0.1), font=2, cex=1.2, col="white")
                  text(37, 95, labels=expression(P[run] > 0.9), font=2, cex=1.2, col="white")
                  points(58,11, pch="o", font=2, cex=1.2, col="white")
                  points(58,5, pch="x", font=2, cex=1.2, col="white")
                  text(24, 63, labels="New Orleans '14", cex=0.8, col="white")
                  text(62, 50, labels="New Orleans '10", cex=0.8, col="white")
                  text(55, 90, labels="New Orleans '98", cex=0.8, col="white")
                  text(70, 58, labels="New Orleans '94", cex=0.8, col="white")
                  text(90, 55, labels="New Orleans '06", cex=0.8, col="white")
                  text(91, 65.83, labels="New Orleans '02", cex=0.8, col="white")
                  arrows(x0=57,x1=53,y0=51,y1=59,length=0.1, col="white")         # '10
                  arrows(x0=54.49,x1=54.49,y0=88,y1=67.5,length=0.1, col="white") # '98                                    
                  arrows(x0=60,x1=56,y0=59,y1=63,length=0.1, col="white")         # '94                                                      
                  arrows(x0=86,x1=80.5,y0=56.5,y1=62,length=0.1, col="white")       # '06   
                  text(69, 33, labels="C=38", font=2, cex=1.2, col="black")       # RIGHT BOX
                  text(14, 80, labels="C=76", font=2, cex=1.2, col="black")       # LEFT BOX
                  points(69,38,  pch=0, cex=3, lwd=2) 
                  points(14,75.5,  pch=0, cex=3,lwd=2)},
               col=rev(scales::alpha(brewer.pal(n=10, name="Spectral"),0.75)), nlevels=10,
               plot.title = title(xlab="M (Racial Margin of Victory)",
                                  ylab="C (% Black Voters)", cex.lab=1.3))

##################################################################
# Manually save the image as a PDF file with width=8 and height=7
##################################################################

#########################################################################################
# END OF THIS R SOURCE FILE
#########################################################################################